package com.jvup.gameshow.model.viewModel.impl;

import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ItemDecoration;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ItemAnimator;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jvup.common.support.base.BaseViewModel;
import com.jvup.common.utils.EquipmentInfoUtil;
import com.jvup.common.basic.StringUtil;
import com.jvup.gameshow.BR;
import com.jvup.gameshow.R;
import com.jvup.gameshow.controller.PlayerShowController;
import com.jvup.gameshow.databinding.LayoutPlayerShowBinding;
import com.jvup.gameshow.databinding.LayoutPlayerShowIdeaBinding;
import com.jvup.gameshow.model.viewModel.IPlayerShowViewModel;
import com.jvup.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.jvup.common.widget.adapter.auto.AutoVariable;
import com.jvup.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.jvup.gameshow.view.adapter.pager.PlayerShowPagerAdapter;
import com.jvup.gameshow.view.attributeBinder.attribute.ImageAttribute;

import java.util.ArrayList;

public abstract class PlayerShowViewModel extends BaseViewModel<IPlayerShowViewModel> implements IPlayerShowViewModel {

    private final ArrayList<PlayerShowZoneViewModel> zoneViewModelList = new ArrayList<>();

    public void initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, PlayerShowController controller){

        zoneViewModelList.add(new PlayerShowZoneViewModel("热门", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("关注", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("游戏", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("娱乐", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("cos", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("漫画", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("热门", inflater, controller));
        zoneViewModelList.add(new PlayerShowZoneViewModel("热门", inflater, controller));

        put("pagerAdapter",new PlayerShowPagerAdapter(zoneViewModelList));

    }

    public class PlayerShowZoneViewModel extends BaseObservable {

        private final ArrayList<PlayerShowIdeaViewModel> ideaList = new ArrayList<>();
        {
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://img0-arch.pconline.com.cn/pconline/1505/12/6449819_003_thumb.jpg"));
            ideaList.add(new PlayerShowIdeaViewModel("http://image.namedq.com/uploads/20190730/22/1564497035-QjLDqgrMhI.jpg"));
        }

        private final int spanCount = 2;
        private final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);

        private final String title;
        private final SummerRecyclerBindAdapter adapter;


        private final LayoutPlayerShowBinding viewBinder;

        private PlayerShowZoneViewModel(String title,@NonNull LayoutInflater inflater, PlayerShowController controller) {
            this.title = title;
            this.adapter = new SummerRecyclerBindAdapter(ideaList,controller);
            viewBinder = DataBindingUtil.inflate(inflater, R.layout.layout_player_show,null,false);
            viewBinder.setPlayerShowZoneViewModel(this);

        }

        public String title(){
            return title;
        }

        public Adapter getAdapter(){
            return adapter;
        }

        public boolean getHasFixedSize(){
            return true;
        }

        public ItemAnimator getItemAnimator(){
            return new DefaultItemAnimator();
        }

        public LayoutManager getLayoutManager(){
            layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
            return layoutManager;
        }

        public ItemDecoration getItemDecoration(){
            return new StaggeredDividerItemDecoration(viewBinder.getRoot().getContext(),10,spanCount);
        }

        public RecyclerView.OnScrollListener getOnScrollListener(){
            return new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        int[] first = new int[spanCount];
                        layoutManager.findFirstCompletelyVisibleItemPositions(first);
                        if (newState == RecyclerView.SCROLL_STATE_IDLE && (first[0] == 1 || first[1] == 1)) {
                            layoutManager.invalidateSpanAssignments();
                        }
                    }
                };
        }

        public class StaggeredDividerItemDecoration extends ItemDecoration {
            private Context context;
            private float interval;
            private int spanCount;

            /**
             * @param interval item的间距
             * @param spanCount 列数
             * */
            public StaggeredDividerItemDecoration(Context context, float interval, int spanCount) {
                this.context = context;
                this.interval = interval;
                this.spanCount = spanCount;
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
                // 获取item在span中的下标
                int spanIndex = params.getSpanIndex();

                int interval = EquipmentInfoUtil.dip2px(this.interval);
//                int interval = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
//                        this.interval, context.getResources().getDisplayMetrics());
                // 中间间隔

                /**
                 * 这个判断适用于瀑布流只有两列的情况，如果有多列，那么再增加spanIndex % spanCount == 的判断并做处理就好了
                 * 此处的left和right都为interval / 2的原因是为了让左边item和右边item同宽
                 * */
                if (spanIndex % spanCount == 0) {
                    outRect.left = interval + EquipmentInfoUtil.dip2px(5);
                    outRect.right = interval / 2;
                } else {
                    outRect.left = interval / 2;
                    outRect.right = interval + EquipmentInfoUtil.dip2px(5);
                }

                // 下方间隔
                outRect.bottom = interval;
            }
        }


        public View getRoot(){
            return viewBinder.getRoot();
        }

    }

    private static int i = 0;
    /**
     * 玩家想法
     */
    @AdapterEntityBind(viewRid = R.layout.layout_player_show_idea)
    public class PlayerShowIdeaViewModel extends BaseObservable implements AutoVariable<LayoutPlayerShowIdeaBinding> {

        private final String id;
        private final String cover;
        private final String title;
        private final int giveLikeCount;

        private final String userId;
        private final String portrait;
        private final String nickname;

        private boolean isLike;


        public PlayerShowIdeaViewModel(String cover) {
            this.id = "";
            this.cover = cover;
            this.title = i++%2==0 ? "测试是事实测试是事实测试是事实测试是事实测试" : "测试是事实是事实测试是事实是事实测试是事实是事实测试是事实是事实测试是事实是事实";
            this.giveLikeCount = 30;

            this.userId = "";
            this.portrait = "https://thirdqq.qlogo.cn/g?b=sdk&k=LVrblgpZUvCEFD9wBP30nQ&s=140&t=1483305885";
            this.nickname = "吃货小猪猪";
        }

        @Override
        public void bindVariable(LayoutPlayerShowIdeaBinding viewBind) {
            viewBind.setPlayerShowIdeaViewModel(this);
        }

        public void toggleGiveLike() {
            isLike = !isLike;
            notifyPropertyChanged(BR.giveLikeIcon);
            notifyPropertyChanged(BR.giveLikeCount);
        }

        public String getId() {
            return id;
        }

        public String getUserId() {
            return userId;
        }

        public ImageAttribute getCover() {
            return new ImageAttribute(cover).setWidth(EquipmentInfoUtil.getScreenWidth()/2);
        }

        public CharSequence getTitle() {
            if(StringUtil.isEmpty(title)) {
                return "";
            } else if(title.length() <= 22) {
                return title;
            }
            return title.substring(0,22) + "...";
        }

        public String getPortrait() {
            return portrait;
        }

        public String getNickname() {
            return nickname;
        }

        @Bindable
        public int getGiveLikeIcon() {
            return isLike ? R.drawable.app_player_show_give_like_a : R.drawable.app_player_show_give_like_b;
        }

        @Bindable
        public String getGiveLikeCount() {
            return String.valueOf(giveLikeCount + (isLike ? 1 : 0));
        }

    }

} 
